package com.zzl.study.auth.security.authentication.context;

import com.zzl.study.auth.utils.JacksonUtils;
import lombok.Data;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;

import java.time.Instant;

/**
 * @author: zhangzl
 * @date: 2024/6/6 14:35
 * @version: 1.0
 * @description: 自定义用户信息
 */
@Data
public class UserInfo {

    private Claims claims;

    private Address address;

    private String locale;

    private String fullName;

    private String zoneInfo;

    private String profile;

    private String preferredUsername;

    private Boolean phoneNumberVerified;

    private String familyName;

    private String nickName;

    private Instant updatedAt;

    private String picture;

    private Boolean emailVerified;

    private String website;

    private String email;

    private String birthdate;

    private String givenName;

    private String middleName;

    private String gender;

    private String phoneNumber;

    private String subject;

    public static OidcUserInfo to(UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        }
        return new OidcUserInfo(JacksonUtils.toMap(userInfo.getClaims()));
    }

    public static UserInfo of(OidcUserInfo oidcUserInfo) {
        if (oidcUserInfo == null) {
            return null;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setClaims(JacksonUtils.toObject(oidcUserInfo.getClaims(), Claims.class));
        userInfo.setAddress(Address.of(oidcUserInfo.getAddress()));
        userInfo.setLocale(oidcUserInfo.getLocale());
        userInfo.setFullName(oidcUserInfo.getFullName());
        userInfo.setZoneInfo(oidcUserInfo.getZoneInfo());
        userInfo.setProfile(oidcUserInfo.getProfile());
        userInfo.setPreferredUsername(oidcUserInfo.getPreferredUsername());
        userInfo.setPhoneNumberVerified(oidcUserInfo.getPhoneNumberVerified());
        userInfo.setFamilyName(oidcUserInfo.getFamilyName());
        userInfo.setNickName(oidcUserInfo.getNickName());
        userInfo.setUpdatedAt(oidcUserInfo.getUpdatedAt());
        userInfo.setPicture(oidcUserInfo.getPicture());
        userInfo.setEmailVerified(oidcUserInfo.getEmailVerified());
        userInfo.setWebsite(oidcUserInfo.getWebsite());
        userInfo.setEmail(oidcUserInfo.getEmail());
        userInfo.setBirthdate(oidcUserInfo.getBirthdate());
        userInfo.setGivenName(oidcUserInfo.getGivenName());
        userInfo.setMiddleName(oidcUserInfo.getMiddleName());
        userInfo.setGender(oidcUserInfo.getGender());
        userInfo.setPhoneNumber(oidcUserInfo.getPhoneNumber());
        userInfo.setSubject(oidcUserInfo.getSubject());
        return userInfo;
    }

}
